﻿using DisconfClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YmatouSeller.Notice.Common
{
    public sealed class AppSettingHelper
    {
        public static string GetAppSetting(string key, string defaultValue)
        {
            var setting = ConfigurationManager.AppSettings[key];
            return string.IsNullOrEmpty(setting) ? defaultValue : setting;
        }
        public static int GetAppSetting(string key, int defaultValue)
        {
            if (string.IsNullOrEmpty(key)) return defaultValue;
            var i = 0;
            int.TryParse(ConfigurationManager.AppSettings[key], out i);
            return i;
        }
        public static DateTime GetAppSetting(string key, DateTime defaultValue)
        {
            if (string.IsNullOrEmpty(key)) return defaultValue;
            DateTime dt = DateTime.Now;
            DateTime.TryParse(ConfigurationManager.AppSettings[key], out dt);
            return dt;
        }

        public static T GetConfigAppSetting<T>(string key, T defaultValue)
        {
            try
            {
                return ConfigManager.AppSettings<T>(key, defaultValue);
            }
            catch
            {
                return defaultValue;
            }
        }

        public static T GetConfigValue<T>(string key, T defaultValue)
        {
            try
            {
                return ConfigManager.GetConfigValue<T>(key, defaultValue);
            }
            catch
            {
                return defaultValue;
            }
        }
    }
}
